package com.qfedu.edu.service.impl;

import com.qfedu.edu.pojo.SecurityUser;
import com.qfedu.edu.pojo.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaobobo
 * @title: MyUserDetailService
 * @projectName CD-Java-FY-2401-Springboot-Parent
 * @description: TODO
 * @date 2024/9/12  10:22
 */
@Service
public class MyUserDetailService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //通过用户名查询用户对象
        if (StringUtils.isEmpty(username)) {
            return null;
        }
        //执行到这里判断这个用户名是不是xiaobobo
        if (!"xiaobobo".equals(username)) {
            //这个就表示的是用户名是xiaobobo
            return null;
        }

        //下面假设这个信息是从数据库查询出来的
        User selectUser=new User(1,"xiaobobo","$2a$10$oI2MhHvkzF0EC2fZMDBLieOA6jWOWy6kgBw9dDkiRq54spu6IGZJG");
        //通过用户id查询权限  查询角色


        //最终共封装成一个UserDetails对象返回

        SecurityUser securityUser = new SecurityUser();
        securityUser.setUser(selectUser);
        //其实这里还需要设置角色和权限

        List<String> objects = new ArrayList<>();
        objects.add("ROLE_admin");
        objects.add("user_query");

        securityUser.setRolesAndPerms(objects);

        return securityUser;
    }
}
